//
//  VideoInfoHeaderView.swift
//  月升
//
//  Created by 拉利法 on 2024/7/11.
//

import UIKit

class VideoInfoHeaderView: UIView {
    
    var previewImg = UIImage()
    var originalImg = UIImage()
    /// 原图在本地服务器地址
    var originalImgUrl = ""
    var event_name = ""
    var Local_Save_Path = ""
    /// 五官检测结果
    var wuGuanModel = QIFiveSensesResultModel()
    /// 原图地址
    var raw_file_path = ""
    /// 收藏ID
    var followID = ""
//    /// 妆容json
    var makeupDict = [String: String]()
    private var followBtn = UIButton()
    private var typeBtn = UIButton()
    private var timeBtn = UIButton()
    private var videoNameL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addUI()
    }
    
    private func addUI() {
        
        videoNameL = UILabel().then({
            $0.text = ""
            $0.textColor = Color333333
            $0.font = FONTBOLD(ofSize: 24)
        })
        self.addSubview(videoNameL)
        videoNameL.mas_makeConstraints { make in
            make?.left.offset()(15)
            make?.top.offset()(10)
            make?.height.offset()(33)
        }
        
        let copyLinkBtn = UIButton().then {
            $0.titleLabel?.font = FONT(ofSize: 14)
            $0.setTitle("复制视频链接", for: .normal)
            $0.setTitleColor(MainColor, for: .normal)
            $0.setImage(UIImage(named: "複製链接"), for: .normal)
            $0.addTarget(self, action: #selector(copLinkBtnClick), for: .touchUpInside)
        }
        self.addSubview(copyLinkBtn)
        copyLinkBtn.mas_makeConstraints({ make in
            make?.left.equalTo()(videoNameL.mas_right)?.offset()(10)
            make?.centerY.equalTo()(videoNameL)
            make?.height.offset()(33)
            make?.width.offset()(105)
        })
        copyLinkBtn.setButtonImagePosition(.left, spacing: 4)
        
        let itemW = (SCREEN_WIDTH - 55) / 3
        let titleArr = ["表演妆容", "约48分钟", "未收藏"]
        for (index, titleStr) in titleArr.enumerated() {
            let btn = UIButton().then {
                $0.frame = CGRect(x: (itemW + 10) * CGFloat(index) + 15, y: 50, width: itemW, height: 30)
                $0.backgroundColor = .white
                $0.titleLabel?.font = FONT(ofSize: 14)
                $0.setTitle(titleStr, for: .normal)
                $0.setTitleColor(MainColor, for: .normal)
                $0.addRoundedCorners(withRadius: 15)
//                $0.setImage(UIImage(named: "<#123#>"), for: .normal)
            }
            self.addSubview(btn)
            
            if index == 0 {
                typeBtn = btn
            } else if index == 1 {
                timeBtn = btn
            } else if index == 2 {
                btn.backgroundColor = MainColor
                btn.setTitleColor(.white, for: .normal)
                btn.setImage(UIImage(named: "已收藏"), for: .selected)
                btn.setImage(UIImage(named: "未收藏"), for: .normal)
                btn.setTitle("已收藏", for: .selected)
                btn.addTarget(self, action: #selector(followBtnClick), for: .touchUpInside)
                followBtn = btn
            }
        }
        
        
        let titleArr2 = ["更换选择条件", "更换风格选择", "AI试装"]
        for (index, titleStr) in titleArr2.enumerated() {
            let btn = UIButton().then {
                $0.frame = CGRect(x: (itemW + 10) * CGFloat(index) + 15, y: followBtn.bottom + 15, width: itemW, height: 100)
                $0.backgroundColor = UIColor(white: 1, alpha: 0.57)
                $0.titleLabel?.font = FONT(ofSize: 14)
                $0.tag = index
                $0.setTitle(titleStr, for: .normal)
                $0.setTitleColor(MainColor, for: .normal)
                $0.addRoundedCorners(withRadius: 6)
                $0.addTarget(self, action: #selector(backBtnClick(btn: )), for: .touchUpInside)
            }
            self.addSubview(btn)
            
            if index == 2 {
                btn.removeTarget(self, action: #selector(backBtnClick(btn: )), for: .touchUpInside)
                btn.addTarget(self, action: #selector(AITrialMakeupClick), for: .touchUpInside)
            }
        }
        
        let titleView = UIView()
        titleView.frame = CGRect(x: 0, y: 220, width: SCREEN_WIDTH, height: 64)
        titleView.backgroundColor = UIColor(white: 1, alpha: 0.57)
        titleView.addRoundedCorners(withRadius: 10, byRoundingCorners: [.topLeft, .topRight])
        self.addSubview(titleView)
        
        let lineView = UIView()
        lineView.frame = CGRect(x: SCREEN_WIDTH / 2 - 30, y: 12, width: 60, height: 8)
        lineView.backgroundColor = ColorWithStr("#DCDCDC")
        lineView.addRoundedCorners(withRadius: 4)
        titleView.addSubview(lineView)
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 15, y: 20, width: 200, height: 25)
            $0.text = "使用的化妆品"
            $0.textColor = Color333333
            $0.font = FONTBOLD(ofSize: 18)
        })
        titleView.addSubview(titleL)
        
    }
    
    var model = QIBeautyRecommenListModel() {
        didSet {
            videoNameL.text = model.title
            typeBtn.setTitle(model.vModel.application, for: .normal)
            timeBtn.setTitle("\(model.vModel.duration)秒", for: .normal)
            followBtn.isSelected = followID.count > 0
        }
    }
    
    @objc private func copLinkBtnClick() {
        let p = UIPasteboard.general
        p.string = model.video_link
        ShowHUD(str: "复制成功")
    }
    
    /// 收藏
    @objc private func followBtnClick() {
        if followBtn.isSelected {
            QINetworkManager.requestJson(urlStr: "/Skin/cancel_collect", parameters: ["collect_id": followID]) { json in
                print(json)
                self.followBtn.isSelected = false
            }
        } else {
            let param = [
                "event_name": event_name,
                "raw_file_path": raw_file_path,
                "facial_data_code_string": wuGuanModel.facial_data_code_string,
                "video_id": model.vModel.id,
                "video": model.video_link,
                "original_drawing": originalImgUrl,
                "name": model.title
            ]
            HUD.flash(.labeledProgress(title: "", subtitle: "收藏中..."), delay: 60)
            QINetworkManager.requestJson(urlStr: "/Skin/collect_video", parameters: param) { json in
                HUD.hide()
                print(json)
                self.followBtn.isSelected = true//!followBtn.isSelected
                self.followID = json.stringValue
                ShowHUD(str: "收藏成功!")
            }
        }
       
    }
    
    @objc private func backBtnClick(btn: UIButton) {
        if btn.tag == 1 {
            self.viewController().navigationController?.popViewController(animated: true)
        } else {
            let ctlArr = self.viewController().navigationController?.viewControllers
            ctlArr?.forEach({ ctl in
                if ctl.classForCoder == AdvancedBeautyViewController.classForCoder() {
                    self.viewController().navigationController?.popToViewController(ctl, animated: true)
                }
            })
        }
        
    }
    /// AI试妆
    @objc private func AITrialMakeupClick() {
        let Makeup_Transfer_Result_Local_Save_Path = "\(makeupDict["Makeup_Transfer_Result_Local_Save_Path"] ?? "")"
        let Cos_Save_Path_Makeup_Transfer = "\(makeupDict["Cos_Save_Path_Makeup_Transfer"] ?? "")"
        
        if Makeup_Transfer_Result_Local_Save_Path.count > 0 && Cos_Save_Path_Makeup_Transfer.count > 0{
            pushCtl(Cos_Save_Path_Makeup_Transfer: Cos_Save_Path_Makeup_Transfer, Makeup_Transfer_Result_Local_Save_Path: Makeup_Transfer_Result_Local_Save_Path)
        } else {
            let url = "/Skin/makeup"
            let param = [
                "choose_video_code": model.vModel.video_code,
                "event_name": event_name,
                "raw_file_path": raw_file_path,
                "facial_data_code_string": wuGuanModel.facial_data_code_string
            ]
            HUD.flash(.labeledProgress(title: "", subtitle: "数据获取中..."), delay: 60)
            QINetworkManager.requestJson(urlStr: url, parameters: param) { json in
                HUD.hide()
                print(json)
                let arr = json["Makeup_Transfer_Result_Local_Save_Path"].arrayValue
                let pathStr = arr.first?.stringValue ?? ""
                let imgArr = json["Cos_Save_Path_Makeup_Transfer"].arrayValue
                let imgUrl = imgArr.first?.stringValue ?? ""
                self.pushCtl(Cos_Save_Path_Makeup_Transfer: imgUrl, Makeup_Transfer_Result_Local_Save_Path: pathStr)
            }
        }
    }
    
    private func pushCtl(Cos_Save_Path_Makeup_Transfer: String, Makeup_Transfer_Result_Local_Save_Path: String) {
        let ctl = AIMakeupViewController()
        ctl.previewImgUrl = Cos_Save_Path_Makeup_Transfer
        ctl.event_name = self.event_name
        ctl.Local_Save_Path = Makeup_Transfer_Result_Local_Save_Path
        ctl.originalImg = self.originalImg
        ctl.videoModel = self.model
        ctl.originalImgUrl = self.originalImgUrl
        self.navigationController?.pushViewController(ctl, animated: true)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//https://moonrise-1301447010.cos.ap-guangzhou.myqcloud.com/makeup_transfer_results/1722577377_70/phpKCx8lV_V02675_final_makeup_transfer_image_with_background.jpg
}



class VideoInfoTableHeaderView: UIView {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        let titleArr = ["推荐", "时间戳", "类型", "品牌", "产品名", "色号"]
        
        let lable1 = getLabel(x: 15, w: 30, titleStr: "推荐")
        let lable2 = getLabel(x: lable1.right + 22, w: 44, titleStr: "时间戳")
        let lable3 = getLabel(x: lable2.right + 40, w: 30, titleStr: "推荐")
        let lable4 = getLabel(x: lable3.right + 23, w: 30, titleStr: "品牌")
        let lable5 = getLabel(x: lable4.right + 55, w: 45, titleStr: "产品名")
        let lable6 = getLabel(x: lable5.right + 55, w: 45, titleStr: "色号")
        
    }
    
    
    private func getLabel(x: CGFloat, w: CGFloat, titleStr: String) -> UILabel {
        let titleL = UILabel().then({
            $0.frame = CGRect(x: x, y: 10, width: w, height: 20)
            $0.text = titleStr
            $0.textColor = Color333333
            $0.font = FONTBOLD(ofSize: 14)
        })
        self.addSubview(titleL)
        return titleL
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
